home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUTORC.ZIP / TUT15.C < prev    next >
C/C++ Source or Header  |  1994-10-30  |  12KB  |  434 lines

  1. /* 
  2.   tut15.c
  3.   10/30/94
  4.   from tutprog15.pas
  5.   Adapted from Denthor's tutprog15.pas
  6.   Translated into C, from Denthor's VGA Trainer, by
  7.   Steve Pinault, scp@ohm.att.com
  8.   Compiled with Microsoft Visual C++ 1.5 (Microsoft C 8.0)
  9.   To compile:
  10.   First compile the subroutines in tutsubs.c with the batch file 
  11.   cltutsub.bat
  12.   Then compile any of the tutor programs with the batch file
  13.   cltut.bat
  14.   Example: C:>cltutsub
  15.            C:>cltut tut15.c
  16.            to compile this program.
  17. */
  18.  
  19. #include "tutheadr.h"
  20.  
  21. #define BIGPAL 26*256
  22. struct RGBType
  23. {
  24.   char r;
  25.   char g;
  26.   char b;
  27. } bob[256],bob2[256],biiiigpallette[BIGPAL];
  28.  
  29. int start,effect,background;
  30. char costbl[256];
  31. char mov1,mov2,mov3,mov4;
  32. char bkg[50][80];
  33.  
  34. //    start:integer;  { Where in the Biiiig pallette are we? }
  35. //    Effect,Background:Boolean; { Configuration of effects }
  36.  
  37. //    costbl : Array [0..255] of byte; { cos table lookup }
  38. //    mov1,mov2,mov3,mov4 : byte;  { current positions }
  39. //    bkg : array [1..50,1..80] of byte; { The pic in the background }
  40.  
  41. //{DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  42. //Procedure Makerun (r,g,b:integer);
  43. //  { This creates a ramp of colors and puts them into biiiigpallette }
  44. void makerun(int r, int g, int b)
  45. {
  46.   int loop1;
  47.   for(loop1=start;loop1<start+128;loop1++)
  48.   {
  49.     if(r==1)
  50.       biiiigpallette[loop1].r=63-(loop1-start)/4;
  51.     else
  52.       if(r==2)
  53.         biiiigpallette[loop1].r=(loop1-start) / 4; 
  54.       else
  55.         biiiigpallette[loop1].r=0;
  56.  
  57.     if(g==1)
  58.       biiiigpallette[loop1].g=63-(loop1-start) / 4 ;
  59.     else
  60.       if(g==2)
  61.         biiiigpallette[loop1].g=(loop1-start) / 4; 
  62.       else
  63.         biiiigpallette[loop1].g=0;
  64.  
  65.     if(b==1)
  66.       biiiigpallette[loop1].b=63-(loop1-start) / 4; 
  67.     else
  68.       if(b==2)
  69.         biiiigpallette[loop1].b=(loop1-start) / 4; 
  70.       else
  71.         biiiigpallette[loop1].b=0;
  72.   }
  73.  
  74.   for(loop1=start+128;loop1<start+256;loop1++)
  75.   {
  76.     if(r==2)
  77.       biiiigpallette[loop1].r=63-(loop1-start)/4;
  78.     else
  79.       if(r==1)
  80.         biiiigpallette[loop1].r=(loop1-start) / 4; 
  81.       else
  82.         biiiigpallette[loop1].r=0;
  83.  
  84.     if(g==2)
  85.       biiiigpallette[loop1].g=63-(loop1-start) / 4 ;
  86.     else
  87.       if(g==1)
  88.         biiiigpallette[loop1].g=(loop1-start) / 4; 
  89.       else
  90.         biiiigpallette[loop1].g=0;
  91.  
  92.     if(b==2)
  93.       biiiigpallette[loop1].b=63-(loop1-start) / 4; 
  94.     else
  95.       if(b==1)
  96.         biiiigpallette[loop1].b=(loop1-start) / 4; 
  97.       else
  98.         biiiigpallette[loop1].b=0;
  99.   }
  100.   start+=256;
  101. }
  102.  
  103.  
  104. //{DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  105. //Procedure init;
  106. void init()
  107. {
  108.   int loop1,loop2,r,g,b;
  109.   int ch;
  110.   char rdch;
  111.   FILE* fptr;
  112.  
  113.   printf("Do you want the Psychadelic effect?: ");
  114.   while(1)
  115.   {
  116.     ch=getch()&0x00ff;
  117.     if(ch=='y')break;
  118.     if(ch=='Y')break;
  119.     if(ch=='n')break;
  120.     if(ch=='N')break;
  121.   }
  122.   if((ch=='y')||(ch=='Y'))
  123.   {
  124.     printf("Yeah!\n");
  125.     effect=TRUE;
  126.   }                
  127.   else
  128.   {
  129.     printf("Nah\n");
  130.     effect=FALSE;
  131.   }
  132.   printf("Do you want the background?: ");
  133.   while(1)
  134.   {
  135.     ch=getch()&0x00ff;
  136.     if(ch=='y')break;
  137.     if(ch=='Y')break;
  138.     if(ch=='n')break;
  139.     if(ch=='N')break;
  140.   }
  141.   if((ch=='y')||(ch=='Y'))
  142.   {
  143.     printf("Yeah!\n");
  144.     background=TRUE;
  145.   }                
  146.   else
  147.   {
  148.     printf("Nah\n");
  149.     background=FALSE;
  150.   }
  151.   printf("Hit any key to continue: ");getch();
  152.   _asm
  153.   {
  154.     mov     ax,0013h
  155.     int     10h          ;           { Enter mode 13 }
  156.     cli
  157.     mov     dx,3c4h
  158.     mov     ax,604h      ;           { Enter unchained mode }
  159.     out     dx,ax
  160.     mov     ax,0F02h     ;           { All planes}
  161.     out     dx,ax
  162.  
  163.     mov     dx,3D4h
  164.     mov     ax,14h       ;           { Disable dword mode}
  165.     out     dx,ax
  166.     mov     ax,0E317h    ;           { Enable byte mode.}
  167.     out     dx,ax        ; so far is same as InitChain4
  168.     mov     al,9
  169.     out     dx,al
  170.     inc     dx
  171.     in      al,dx
  172.     and     al,0E0h      ;  320x400 mode, see Abrash pg. 207.{ Duplicate each scan 8 times.}
  173.     add     al,7
  174.     out     dx,al
  175.   }
  176.  
  177.   _fmemset(&bob2[0],0,768);
  178.   SetAllPal((char far*)&bob2[0]);
  179.  
  180.   start=0;
  181.   r=0;
  182.   g=0;
  183.   b=0;
  184.   while(1)
  185.   {
  186.     makerun (r,g,b);
  187.     b++;
  188.     if(b==3)
  189.     {
  190.       b=0;
  191.       g++;
  192.     }
  193.     if(g==3)
  194.     {
  195.       g=0;
  196.       r++;
  197.     }
  198.     if((r==2)&&(g==2)&&(b==2))break;
  199.   }
  200.   // { Set up our major run of colors }
  201.   start=0;
  202.   if (!effect)
  203.   {
  204.     for(loop1=0;loop1<128;loop1++)
  205.     {
  206.       bob[loop1].r=63-loop1 / 4;
  207.       bob[loop1].g=0;
  208.       bob[loop1].b=loop1 / 4;
  209.     }
  210.     for( loop1=128;loop1<256;loop1++)
  211.     {
  212.       bob[loop1].r=loop1 / 4;
  213.       bob[loop1].g=0;
  214.       bob[loop1].b=63-loop1 / 4;
  215.     }
  216.   }
  217.   else
  218.     for(loop1=0;loop1<256;loop1++)
  219.     {
  220.       bob[loop1].r=biiiigpallette[loop1].r;
  221.       bob[loop1].g=biiiigpallette[loop1].g;
  222.       bob[loop1].b=biiiigpallette[loop1].b;
  223.     }
  224.  
  225.     //{ Set up a nice looking pallette ... we alter color 0, so the border will
  226.     //  be altered. }
  227.  
  228.   for(loop1=0;loop1<256;loop1++)
  229.     costbl[loop1]=(char)round ((float)cos ((double)rad ((int)((long)loop1*(long)(255*2)/(long)360)))*31)+32;
  230.     // { Set up our lookup table...}
  231.  
  232.   _fmemset(bkg,0,sizeof(bkg));
  233.   if((fptr=fopen("bkg.dat","r"))==NULL)
  234.      {SetText(); printf("Error opening file bkg.dat\n"); exit(1);}
  235.   for(loop1=0;loop1<50;loop1++)
  236.   {
  237.     for(loop2=0;loop2<80;loop2++)
  238.     {
  239.       fread(&rdch,sizeof(rdch),1,fptr);
  240.       if(rdch!=48)bkg[loop1][loop2]=rdch-28;
  241.     }
  242.     fread(&rdch,sizeof(rdch),1,fptr); // read newline
  243.     fread(&rdch,sizeof(rdch),1,fptr); // newline is 2 bytes!
  244.   }
  245.   fclose (fptr);
  246.   //{ Here we read in our background from the file bkg.dat }
  247. }
  248.  
  249.  
  250. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  251. //Procedure DrawPlasma;
  252. //  { This procedure draws the plasma onto the screen }
  253. void drawplasma()
  254. {
  255.   int loop1,loop2;
  256.   char tmov1,tmov2,tmov3,tmov4;
  257.   char col;
  258.   int where;
  259.  
  260.   tmov3=mov3;
  261.   tmov4=mov4;
  262.   where=0;
  263.   __asm
  264.   {
  265.     mov   ax,0a000h
  266.     mov   es,ax    ;    { In the two loops that follow, ES is not altered so
  267.   }                   //    we just set it once, now }
  268.   for(loop1=0;loop1<50;loop1++) // { Fifty rows down }
  269.   {
  270.     tmov1=mov1;
  271.     tmov2=mov2;
  272.     for(loop2=0;loop2<80;loop2++)  //{ Eighty columns across }
  273.     {
  274.       if(background)
  275.         col=costbl[tmov1]+costbl[tmov2]+costbl[tmov3]+costbl[tmov4]+costbl[loop1]+costbl[loop2]+bkg[loop1][loop2];
  276.       else
  277.         col=costbl[tmov1]+costbl[tmov2]+costbl[tmov3]+costbl[tmov4]+costbl[loop1]+costbl[loop2];
  278.         // { col = Intersection of numerous cos waves }
  279.       __asm
  280.       {
  281.         mov    di,where   ; { di is killed elsewhere, so we need to restore it}
  282.         mov    al,col
  283.         mov    es:[di],al ; { Place col at ES:DI ... sequential across the screen}
  284.       }
  285.       where++;  // { Inc the place to put the pixel }
  286.       tmov1+=4;
  287.       tmov2+=3; // { Arb numbers ... replace to zoom in/out }
  288.     }
  289.     tmov3+=4;
  290.     tmov4+=5;   // { Arb numbers ... replace to zoom in/out }
  291.   }
  292. }
  293.  
  294. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  295. // Procedure MovePlasma;
  296. //  { This procedure moves the plasma left/right/up/down }
  297. void moveplasma()
  298. {
  299.   mov1-=4;
  300.   mov3+=4;
  301.   mov1+=random (2);  // Note my random is 0 <= (int) random((int)x) < x
  302.   mov2-=random (3);
  303.   mov3+=random (2);
  304.   mov4-=random (3);  // { Movement along the plasma + noise}
  305. }
  306.  
  307. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  308. // Procedure fadeupone (stage:integer);
  309. //  { This procedure fades up the pallette bob2 by one increment and sets the
  310. //    onscreen pallette. Colors are increased proportionally, do that all colors
  311. //    reach their destonation at the same time }
  312. void fadeupone(int stage)
  313. {
  314.   int loop1;
  315.   struct RGBType temp;
  316.   if(!effect)_fmemmove(&temp,&bob[0],3);
  317.   _fmemmove(&bob[0],&bob[1],765);
  318.   if(effect)_fmemmove (&bob[255],&biiiigpallette[start],3);
  319.   else
  320.     _fmemmove (&bob[255],&temp,3);
  321.   start++;
  322.   if(start==BIGPAL) start=0;
  323.     // { Rotate the pallette }
  324.  
  325.   for(loop1=0;loop1<256;loop1++)
  326.   {
  327.     bob2[loop1].r=(bob[loop1].r*stage) / 64;
  328.     bob2[loop1].g=(bob[loop1].g*stage) / 64;
  329.     bob2[loop1].b=(bob[loop1].b*stage) / 64;
  330.   } // { Fade up the pallette }
  331.   SetAllPal ((char far*)&bob2[0]);
  332. }
  333.  
  334.  
  335. //{DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  336. //Procedure Shiftpallette;
  337. //  { This rotates the pallette, and introduces new colors if the psychadelic
  338. //    effect has been chosen }
  339. void shiftpalette()
  340. {
  341.   struct RGBType temp;
  342.   if(!effect)_fmemmove(&temp,&bob2[0],3);
  343.   _fmemmove(&bob2[0],&bob2[1],765);
  344.   if(effect)_fmemmove (&bob2[255],&biiiigpallette[start],3) ;
  345.   else
  346.     _fmemmove (&bob2[255],&temp,3);
  347.   start++;
  348.   if(start==BIGPAL)start=0;
  349.   SetAllPal ((char far*)&bob2[0]);
  350. }
  351.  
  352. //{DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  353. //Procedure Play;
  354. void play()
  355. {
  356.   int loop1;
  357.   start=256;
  358.   for(loop1=1;loop1<65;loop1++)
  359.   {
  360.     fadeupone(loop1);
  361.     drawplasma();
  362.     moveplasma();
  363.   } // { Fade up the plasma }
  364.   while(1)
  365.   {
  366.     shiftpalette();
  367.     drawplasma();
  368.     moveplasma();
  369.     if(_bios_keybrd(_KEYBRD_READY))break;
  370.   }
  371.   getch();
  372.   _fmemmove (&bob[0],&bob2[0],768);
  373.   for(loop1=1;loop1<65;loop1++)
  374.   {
  375.     fadeupone(64-loop1);
  376.     drawplasma();
  377.     moveplasma();
  378.   } // { fade down the plasma }
  379. }
  380.  
  381. void main()
  382. {
  383.   init();
  384.   play();
  385.   __asm
  386.   {
  387.     mov  ax,0003h
  388.     int  10h
  389.   }
  390. }           
  391.  
  392. /*
  393. BEGIN
  394.   clrscr;
  395.   writeln ('Hi there ... here is a tut on plasmas! (By popular demand). The');
  396.   writeln ('program will ask you weather you want the Psychadelic effect, in');
  397.   writeln ('which the pallette does strange things (otherwise the pallette');
  398.   writeln ('remains constant), and it will ask weather you want a background');
  399.   writeln ('(a static pic behind the plasma). Try them both!');
  400.   writeln;
  401.   writeln ('The thing about plasmas is that they are very easy to change/modify');
  402.   writeln ('and this one is no exception .. you can even change the background');
  403.   writeln ('with minimum hassle. Try adding and deleting things, you will be');
  404.   writeln ('surprised by the results!');
  405.   writeln;
  406.   writeln ('This is by no means the only way to do plasmas, and there are other');
  407.   writeln ('sample programs out there. Have fun with this one though! ;-)');
  408.   writeln;
  409.   writeln;
  410.   init;
  411.   play;
  412.   asm
  413.     mov  ax,0003h
  414.     int  10h
  415.   end;
  416.   Writeln ('All done. This concludes the fifteenth sample program in the ASPHYXIA');
  417.   Writeln ('Training series. You may reach DENTHOR under the names of GRANT');
  418.   Writeln ('SMITH/DENTHOR/ASPHYXIA on the ASPHYXIA BBS.I also occasinally');
  419.   Writeln ('RSAProg, comp.lang.pascal and comp.sys.ibm.pc.demos. E-mail me at :');
  420.   Writeln ('    denthor@beastie.cs.und.ac.za');
  421.   Writeln ('The numbers are available in the main text. You may also write to me at:');
  422.   Writeln ('             Grant Smith');
  423.   Writeln ('             P.O. Box 270');
  424.   Writeln ('             Kloof');
  425.   Writeln ('             3640');
  426.   Writeln ('             Natal');
  427.   Writeln ('             South Africa');
  428.   Writeln ('I hope to hear from you soon!');
  429.   Writeln; Writeln;
  430.   Write   ('Hit any key to exit ...');
  431.   readkey;
  432. END.
  433. */
  434.